//
//  InformationNewsRootViewController.swift
//  KaMengApplication
//
//  Created by DUONIU_MAC on 2018/10/24.
//  Copyright © 2018年 DUONIU_MAC. All rights reserved.
//

import UIKit

class InformationNewsRootViewController: KMViewController {
    
    //头部视图
    fileprivate var headView : NewsHeadView!
    
    //综合资讯
    fileprivate var headLineView = HeadlineView()

    fileprivate var informationVC : InformationViewController!

    override func viewDidLoad() {
        super.viewDidLoad()

        createHeadView()
        NotificationCenter.default.addObserver(self, selector: #selector(showHeadline), name: .informationPush, object: nil)
        NotificationCenter.default.addObserver(self, selector: #selector(popularWoolPush(sender:)), name: .popularWoolPush, object: nil)
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        
    }
    
    deinit {
        
        NotificationCenter.default.removeObserver(self, name: .informationPush, object: nil)
        NotificationCenter.default.removeObserver(self, name: .popularWoolPush, object: nil)
    }
    
    //MARK: 热门羊毛
    @objc fileprivate func popularWoolPush(sender:Notification) {
        
        let index = Int((sender.userInfo!["index"] as? String)!)
        self.moveToViewController(index: index!)
    }
    
    //MARK: 显示综合模块
    @objc fileprivate func showHeadline() {
        
        headLineView.isHidden = false
        self.view.bringSubview(toFront: headLineView)
    }

    
    //MARK: 添加头部视图
    fileprivate func createHeadView() {
        
        headView = NewsHeadView.init(frame: .zero)
        headView.newsDelegate = self
        headView.imageUrl = url_informationNews
        self.view.addSubview(headView)
        
        headLineView.headView.moreButton.addTarget(self, action: #selector(moreLabel), for: .touchUpInside)
        headLineView.urlString = url_informationNews
        headLineView.delegate = self
        headLineView.titleArray = [todayHeadlines,featuredFavourites,littleWhiteRaiders,useCardRaiders,quotaStrategy,creditCardEvaluation,popularWool,exclusiveRecommendation,specialReport,newCardExpress,bankInformation]
    
        self.view.addSubview(headLineView)
        
        if headView.dataArray.count > 0 {
            headView.snp.makeConstraints { (make) in
                make.top.left.right.equalToSuperview()
                make.height.equalTo(RATIO_H(maxNum: 200))
            }
        }
        
        headLineView.snp.makeConstraints { (make) in
            make.top.equalTo(headView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        

    }
    
    //MARK: 更多标签
    @objc fileprivate func moreLabel() {
        
        let dataArray = [todayHeadlines,featuredFavourites,littleWhiteRaiders,useCardRaiders,quotaStrategy,creditCardEvaluation,popularWool,exclusiveRecommendation,specialReport,newCardExpress,bankInformation]
        
        let moreLabelVC = MoreLabelViewController.init(dataArray: dataArray)
        moreLabelVC.labelBlock = {(index) in
            self.moveToViewController(index: index.row)
        }
        self.present(moreLabelVC, animated: false, completion: nil)
    }
    
    //AMRK: 创建新闻详情页
    public func moveToViewController(index:Int) {
    
        //隐藏综合新闻
        headLineView.isHidden = true
        
        if informationVC == nil {
            informationVC = InformationViewController()
            
            informationVC.InformationDelegate = self
            
            self.addChildViewController(informationVC)
            self.didMove(toParentViewController: self)
            
            self.view.addSubview(informationVC.view)
            
            informationVC.view.snp.makeConstraints { (make) in
                make.top.equalTo(headView.snp.bottom)
                make.left.right.bottom.equalToSuperview()
            }
        }
        
        informationVC.moveToViewController(at: index)
        
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
        
    }

}

extension InformationNewsRootViewController : NewsHeadViewDelegate {
    
    //MARK: 改变头部视图坐标
    func changeHeadViewFrame() {
        
        headView.snp.remakeConstraints { (make) in
            make.top.equalToSuperview()
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 200))
        }
    }
    
    //MARK: 选择轮播动画
    func selectTitleImage(model: NewsPageModel) {
        
        let url = "https:" + model.href!
        let webVC = WebViewController.init(urlString: url, title: model.title, shareImage: kmHttps + model.image!, shareText: nil, shareTitle: model.title)
        webVC.hidesBottomBarWhenPushed = true
        webVC.removeElements = ["mbloc mblocks","crumbs mblock","app-download"]
        webVC.removeIds = ["xiong_div","geturl"]
        webVC.isCarouselDiagram = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    

}

extension InformationNewsRootViewController : InformationDelegate,HeadlineViewDelegate {
    
    func updateHeadlineTiltle() {
        
        self.updateTitle()
    }
    
    //MARK: 刷新标题
    func updateTitle() {
        
        if headView.dataArray.count == 0 {
            self.headView.getData()
        }
        
    }
    
    //MARK: 改变坐标
    func changeScrollHeight(height: CGFloat) {
        
        if self.headView.dataArray.count == 0 {
            return
        }
        
        var y:CGFloat = height
        if height >= 0 {
            
            if height > RATIO_H(maxNum: 200) {
                y = RATIO_H(maxNum: 200)
            }
        }else{
            
            y = 0
        }
        
        
        headView.snp.remakeConstraints { (make) in
            make.top.equalToSuperview().offset(-y)
            make.left.right.equalTo(self.view)
            make.height.equalTo(RATIO_H(maxNum: 200))
        }
    }
    
    //MARK: 滑动到指定位置
    func scrollToFrame(y: CGFloat) {
        
        self.changeScrollHeight(height: y)
    }
    
    //MARK: 选择标签
    func selectItem(index: Int) {
        
        self.moveToViewController(index: index)
    }
    
    //MARK: 选择新闻
    func selectNews(model: NewsModel) {
        
        let webVC = WebViewController.init(urlString: kmHttps + model.href!, title: information_title + synthesis, shareImage: kmHttps + model.image!, shareText: model.text,shareTitle:model.title)
        webVC.removeElements = ["mblocks","crumbs mblock","app-download","mfooter mblock footer1","bshare-custom icon-medium"]
        webVC.removeIds = ["xiong_div"]
        webVC.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(webVC, animated: true)
    }
    
}
